package com.neusoft.elm.servlet;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.neusoft.elm.po.Business;
import com.neusoft.elm.service.BusinessService;
import com.neusoft.elm.service.impl.BusinessServiceImpl;

@WebServlet("/BusinessRegisterServlet")
public class BusinessRegisterServlet extends HttpServlet {
    private static final long serialVersionUID = 1L;
    
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        // 设置响应编码
        response.setContentType("application/json;charset=UTF-8");
        response.setCharacterEncoding("UTF-8");
        
        PrintWriter out = response.getWriter();
        ObjectMapper mapper = new ObjectMapper();
        
        try {
            // 获取请求参数
            String businessIdStr = request.getParameter("businessId");
            String password = request.getParameter("password");
            String businessName = request.getParameter("businessName");
            String businessAddress = request.getParameter("businessAddress");
            String businessExplain = request.getParameter("businessExplain");
            String starPriceStr = request.getParameter("starPrice");
            String deliveryPriceStr = request.getParameter("deliveryPrice");
            
            // 参数验证
            if (businessIdStr == null || businessIdStr.trim().isEmpty()) {
                out.print(mapper.writeValueAsString(new RegisterResult(false, "商家账号不能为空")));
                return;
            }
            
            if (password == null || password.trim().isEmpty()) {
                out.print(mapper.writeValueAsString(new RegisterResult(false, "密码不能为空")));
                return;
            }
            
            if (businessName == null || businessName.trim().isEmpty()) {
                out.print(mapper.writeValueAsString(new RegisterResult(false, "商家名称不能为空")));
                return;
            }
            
            if (businessAddress == null || businessAddress.trim().isEmpty()) {
                out.print(mapper.writeValueAsString(new RegisterResult(false, "商家地址不能为空")));
                return;
            }
            
            if (businessExplain == null || businessExplain.trim().isEmpty()) {
                out.print(mapper.writeValueAsString(new RegisterResult(false, "商家介绍不能为空")));
                return;
            }
            
            if (starPriceStr == null || starPriceStr.trim().isEmpty()) {
                out.print(mapper.writeValueAsString(new RegisterResult(false, "起送费不能为空")));
                return;
            }
            
            if (deliveryPriceStr == null || deliveryPriceStr.trim().isEmpty()) {
                out.print(mapper.writeValueAsString(new RegisterResult(false, "配送费不能为空")));
                return;
            }
            
            // 转换数据类型
            Integer businessId;
            Double starPrice;
            Double deliveryPrice;
            
            try {
                businessId = Integer.valueOf(businessIdStr.trim());
                starPrice = Double.valueOf(starPriceStr.trim());
                deliveryPrice = Double.valueOf(deliveryPriceStr.trim());
            } catch (NumberFormatException e) {
                out.print(mapper.writeValueAsString(new RegisterResult(false, "数据格式错误")));
                return;
            }
            
            // 检查商家ID是否已存在
            BusinessService businessService = new BusinessServiceImpl();
            Business existingBusiness = businessService.getBusinessById(businessId);
            if (existingBusiness != null) {
                out.print(mapper.writeValueAsString(new RegisterResult(false, "商家账号已存在")));
                return;
            }
            
            // 创建商家对象
            Business business = new Business();
            business.setBusinessId(businessId);
            business.setPassword(password);
            business.setBusinessName(businessName);
            business.setBusinessAddress(businessAddress);
            business.setBusinessExplain(businessExplain);
            business.setStarPrice(starPrice);
            business.setDeliveryPrice(deliveryPrice);
            
            // 调用业务层保存商家
            int result = businessService.saveBusiness(business);
            
            if (result > 0) {
                out.print(mapper.writeValueAsString(new RegisterResult(true, "注册成功")));
            } else {
                out.print(mapper.writeValueAsString(new RegisterResult(false, "注册失败，请稍后重试")));
            }
            
        } catch (Exception e) {
            e.printStackTrace();
            out.print(mapper.writeValueAsString(new RegisterResult(false, "系统错误，请稍后重试")));
        } finally {
            out.close();
        }
    }
    
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        doPost(request, response);
    }
    
    // 注册结果类
    public static class RegisterResult {
        private boolean success;
        private String message;
        
        public RegisterResult() {}
        
        public RegisterResult(boolean success, String message) {
            this.success = success;
            this.message = message;
        }
        
        public boolean isSuccess() {
            return success;
        }
        
        public void setSuccess(boolean success) {
            this.success = success;
        }
        
        public String getMessage() {
            return message;
        }
        
        public void setMessage(String message) {
            this.message = message;
        }
    }
}